﻿using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace PlurkApi
{
    /// <summary>
    ///  a plurk message
    /// </summary>
    public class plurk
    {
        #region Properties

        /// <summary>
        /// the language of the plurk message
        /// </summary>
        public string lang { get; set; }

        /// <summary>
        /// The raw content as user entered it, useful when editing plurks or if you want to format the content differently. 
        /// </summary>
        public string content_raw { get; set; }

        /// <summary>
        /// The English qualifier, can be "says", show all: 
        /// loves likes shares gives hates wants has will asks wishes was feels thinks says is : freestyle hopes needs wonders
        /// </summary>
        public string qualifier { get; set; }

        /// <summary>
        /// Only set if the language is not English, will be the translated qualifier. Can be "siger" if plurk.lang is "da" (Danish). 
        /// </summary>
        public string qualifier_translated { get; set; }

        /// <summary>
        /// The formatted content, emoticons and images will be turned into IMG tags etc. 
        /// </summary>
        public string content { get; set; }

        /// <summary>
        /// the users who could read the plurk message
        /// </summary>
        public string limited_to { get; set; }

        /// <summary>
        /// The unique Plurk id, used for identification of the plurk. 
        /// </summary>
        public long plurk_id { get; set; }

        /// <summary>
        /// Who is the owner/poster of this plurk. 
        /// </summary>
        public long owner_id { get; set; }

        /// <summary>
        /// How many responses does the plurk have. 
        /// </summary>
        public int response_count { get; set; }

        /// <summary>
        /// How many of the responses have the user read. This is automatically updated when fetching responses or marking a plurk as read. 
        /// </summary>
        public int responses_seen { get; set; }

        /// <summary>
        /// Specifies what type of plurk it is and if the plurk has been responded by the user. The value of plurk_type is only correct when calling getPlurks with only_responded filter (this is done for perfomance and caching reasons). 
        /// plurk_type=0 //Plublic plurk
        /// plurk_type=1 //Private plurk
        /// plurk_type=2 //Plublic plurk (responded by the logged in user)
        /// plurk_type=3 //Private plurk (responded by the logged in user)
        /// </summary>
        public int plurk_type { get; set; }

        /// <summary>
        /// Which timeline does this Plurk belong to. 
        /// </summary>
        public long user_id { get; set; }

        /// <summary>
        /// If set to 1, then responses are disabled for this plurk.
        /// If set to 2, then only friends can respond to this plurk. 
        /// </summary>
        public int no_comments { get; set; }

        public long id { get; set; }

        /// <summary>
        /// Specifies if the plurk is read, unread or muted
        /// is_unread=0 //Read
        /// is_unread=1 //Unread
        /// is_unread=2 //Muted
        /// </summary>
        public int? is_unread { get; set; }

        /// <summary>
        /// The date this plurk was posted. 
        /// </summary>
        public DateTime posted { get; set; }

        /// <summary>
        ///  the id list of  plurkers who like this plurk
        /// </summary>
        public List<long> favorers { get; set; }

        /// <summary>
        ///  you like this plurk or not
        /// </summary>
        public bool? favorite { get; set; }

        /// <summary>
        ///  the number of plurkers favorite this plurk
        /// </summary>
        public int? favorite_count { get; set; }

        /// <summary>
        /// Gets the permalink.
        /// </summary>
        public string permalink
        {
            get { return string.Format("http://www.plurk.com/p/{0}", PlurkApi.ConvertToBase(plurk_id, 36)); }
        }

        #endregion
      
        /// <summary>
        /// Initializes a new instance of the <see cref="plurk"/> class.
        /// </summary>
        /// <param name="jsonString">The json string.</param>
        public plurk(string jsonString)
        {
            var obj = JObject.Parse(jsonString);
            if (obj.Count <= 0) { return; }

            lang = (obj["lang"] == null) ? string.Empty : (string)obj["lang"];
            content_raw = (obj["content_raw"] == null) ? string.Empty : (string)obj["content_raw"];
            qualifier = (obj["qualifier"] == null) ? string.Empty : (string)obj["qualifier"];
            qualifier_translated = (obj["qualifier_translated"] == null) ? string.Empty : (string)obj["qualifier_translated"];
            content = (obj["content"] == null) ? string.Empty : (string)obj["content"];
            limited_to = (obj["limited_to"] == null) ? string.Empty : (string)obj["limited_to"];

            plurk_id = (obj["plurk_id"] == null) ? 0 : (long)obj["plurk_id"];
            owner_id = (obj["owner_id"] == null) ? 0 : (long)obj["owner_id"];
            response_count = (obj["response_count"] == null) ? 0 : (int)obj["response_count"];
            responses_seen = (obj["responses_seen"] == null) ? 0 : (int)obj["responses_seen"];
            plurk_type = (obj["plurk_type"] == null) ? 0 : (int)obj["plurk_type"];
            user_id = (obj["user_id"] == null) ? 0 : (long)obj["user_id"];
            no_comments = (obj["no_comments"] == null) ? 0 : (int)obj["no_comments"];
            id = (obj["id"] == null) ? 0 : (long)obj["id"];
            is_unread = (obj["is_unread"] == null) ? null : (int?)obj["is_unread"];

            favorers = (obj["favorers"] == null) ? new List<long>() : JsonConvert.DeserializeObject<List<long>>(obj["favorers"].ToString());
            favorite = (obj["favorite"] == null || obj["favorite"].ToString() == "null") ? false : (bool)obj["favorite"];
            favorite_count = (obj["favorite_count"] == null) ? null : (int?)obj["favorite_count"];

            if (obj["posted"] != null) { posted = DateTime.Parse(obj["posted"].ToString().Replace("\"", "")); }
        }
    }

    /// <summary>
    /// a colleciton of plurk messages
    /// </summary>
    public class plurks : List<plurk> { }
}